package com.gs.m_my_goods

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import com.gs.common.BaseCompatActivity
import com.gs.common.constant.ActionConst
import com.gs.common.data.Good
import com.gs.common.global.GlobalVariables
import com.gs.common.util.ActionBarUtil
import com.gs.common.util.DialogUtil
import com.gs.common.util.ToastUtil
import com.gs.m_my_goods.adapter.MyGoodsAdapter
import com.gs.m_my_goods.databinding.MyGoodsLayoutBinding
import com.gs.m_my_goods.persistent.MyGoodsPersistent

class MyGoodsActivity: BaseCompatActivity(), MyGoodsPersistent.OnDoGetGoodsStateCallback {

    private lateinit var binding: MyGoodsLayoutBinding
    private val myGoodsPersistent by lazy { MyGoodsPersistent.getInstance(this) }
    private val mGoodList = ArrayList<Good>()
    private val mLoginUser = GlobalVariables.loginUser

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = MyGoodsLayoutBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.loadingTv.visibility = View.VISIBLE
        binding.myGoodsList.visibility = View.GONE


        // 自定义 action bar
        ActionBarUtil.setCustomActionBar(this, R.layout.my_goods_list_actionbar_layout)

        // 设置返回按键
        val backBtn = findViewById<ImageView>(R.id.back_btn)
        backBtn.setOnClickListener {
            finish()
        }

        // 设置添加商品案件
        val addGoodBtn = findViewById<ImageView>(R.id.add_good_btn)
        addGoodBtn.setOnClickListener {
            val intent = Intent().apply {
                action = ActionConst.TO_ADD_OR_UPDATE_GOOD_ACTIVITY_ACTION
                removeExtra("state")
                putExtra("state", "添加")
            }
            startActivity(intent)
        }
    }

    override fun onResume() {
        super.onResume()

        // 配置 order list recyclerView
        val layoutManager = LinearLayoutManager(this)
        binding.myGoodsList.layoutManager = layoutManager
        val adapter = MyGoodsAdapter(this, mGoodList)
        binding.myGoodsList.adapter = adapter

        // 显示order list，不显示加载数据中
        binding.loadingTv.visibility = View.GONE
        updateMyGoodsData()
        binding.myGoodsList.visibility = View.VISIBLE


        adapter.setMyGoodsItemUpdateBtnClickListener(object : MyGoodsAdapter.OnMyGoodsItemUpdateBtnClickListener{
            override fun onClick(position: Int, goodId: Int) {
                val intent = Intent().apply {
                    action = ActionConst.TO_ADD_OR_UPDATE_GOOD_ACTIVITY_ACTION
                    removeExtra("state")
                    putExtra("state", "修改")
                    putExtra("goodId", goodId.toString())
                }
                startActivity(intent)
            }
        })
        adapter.setMyGoodsItemDeleteBtnClickListener(object : MyGoodsAdapter.OnMyGoodsItemDeleteBtnClickListener{
            override fun onClick(position: Int, goodId: Int) {
                val alertDialog =  DialogUtil.createCommonAlertDialog(this@MyGoodsActivity, "删除", "这个商品") {
                    if (it == DialogUtil.CONFIRM_TO_DO) {
                        myGoodsPersistent.deleteGood(goodId, object : MyGoodsPersistent.OnDeleteGoodStateCallback{
                            override fun onDeleteGoodSuccess() {
                                updateMyGoodsData()
                                ToastUtil.showShortToast(this@MyGoodsActivity, "删除成功")
                            }

                            override fun onDeleteGoodFailed() {
                                ToastUtil.showShortToast(this@MyGoodsActivity, "删除失败")
                            }
                        })
                    }
                }
                alertDialog.show()
            }
        })
    }

    private fun updateMyGoodsData() {
        if (mLoginUser == null) {
            ToastUtil.showShortToast(this, "更新我的商品失败")
            return
        }
        mGoodList.clear()
        myGoodsPersistent.getGoodsByUsername(mLoginUser.userName, this)
    }

    override fun onGetGoodsSuccess() {
        val goodsList = GlobalVariables.goodsList
        if (goodsList == null) {
            ToastUtil.showShortToast(this@MyGoodsActivity, "更新我的商品失败")
            return
        }
        if (goodsList.isEmpty()) {
            binding.emptyMyGoodsListTv.visibility = View.VISIBLE
        } else {
            binding.emptyMyGoodsListTv.visibility = View.GONE
        }
        mGoodList.clear()
        for (good in goodsList) {
            mGoodList.add(good)
        }

        binding.myGoodsList.adapter?.notifyDataSetChanged()
    }

    override fun onGetGoodsFailed() {
        ToastUtil.showShortToast(this, "更新我的商品失败")
    }
}